package com.example.park2212.points

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.adapter.RepairAdapter
import com.example.park2212.databinding.ActivityDetailPointsBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/points/DetailPointsActivity")
class DetailPointsActivity : BaseActivity<ActivityDetailPointsBinding,PointsViewModel>() {

    @Autowired
    lateinit var pid:String

    val standList = listOf<String>("1 绿地是否需要修剪", "2 现场是否有垃圾", "3 是否有安全隐患", "4 设施是否有损坏")
    lateinit var ponitStandAdapter: PonitStandAdapter
    lateinit var taskAdapter: TaskAdapter
    val repairAdapter by lazy { RepairAdapter() }

    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.getPointsById(pid.toInt())

        binding.headPointdetail.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("点位详情")
            headerAdd.visibility = View.GONE
        }

        binding.pointdetailTab.apply {
            addTab(newTab().setText("巡检标准"))
            addTab(newTab().setText("巡检记录"))
            addTab(newTab().setText("工单记录"))
            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    when(tab?.position){
                        0 ->
                            binding.rvPointdetail.adapter = ponitStandAdapter
//                            binding.rvPointdetail.apply {
//                                adapter = ponitStandAdapter
//                                addItemDecoration(
//                                    DividerItemDecoration(
//                                        context,
//                                        DividerItemDecoration.VERTICAL
//                                    )
//                                )
//                            }

                        1 ->
                            binding.rvPointdetail.adapter = taskAdapter
//                            binding.rvPointdetail.apply {
//                                adapter = taskAdapter
//                            }

                        2 ->
                            Log.i("===", "查询工单记录")
                            //binding.rvPointdetail.adapter = repairAdapter
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {

                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })
        }

        taskAdapter = TaskAdapter()
        ponitStandAdapter = PonitStandAdapter()
        binding.rvPointdetail.adapter = ponitStandAdapter
        binding.rvPointdetail.layoutManager = LinearLayoutManager(this)
        ponitStandAdapter.addData(standList)




        viewModel.getTasks()
        viewModel.getOrder()


    }

    override fun initData() {
        viewModel.getPointById.observe(this){
            binding.bean = it
            binding.pointdetailStatus.apply {
                if (it.patrolType == "保洁巡检"){
                    setBackgroundResource(R.drawable.shape_weihu)
                }else if (it.patrolType == "安防巡检"){
                    setBackgroundResource(R.drawable.shape_zhanyong)
                }else{
                    setBackgroundResource(R.drawable.shape_status)
                }
            }
        }

        viewModel.getTasks.observe(this){
            taskAdapter.addData(it)
            taskAdapter.notifyDataSetChanged()
        }

        viewModel.getOrderSuccess.observe(this){
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }


    }


}